/*
 * Copyright (c) 2013 Hai Bison
 *
 * See the file LICENSE at the root directory of this project for copying
 * permission.
 */

package com.haibison.android.logs.providers;

import android.database.DatabaseUtils;
import android.database.MatrixCursor;
import android.provider.BaseColumns;

/**
 * Database utilities.
 * 
 * @author Hai Bison
 * @since v1.0 beta
 */
public class DbUtils {

    /**
     * This is singleton class.
     */
    private DbUtils() {
    }// DbUtils()

    /**
     * Formats {@code n} to text to store to database. This method prefixes the
     * output string with {@code "0"} to make sure the result will always have
     * same length (for a {@link Long}). So it will work when comparing
     * different values as text.
     * <p/>
     * <b>Note:</b> This doesn't escape the result. If you need to escape SQL,
     * use {@link #formatNumber(long, boolean)}.
     * 
     * @param n
     *            a long value.
     * @return the formatted string.
     */
    public static String formatNumber(long n) {
        return formatNumber(n, false);
    }// formatNumber()

    /**
     * Formats {@code n} to text to store to database. This method prefixes the
     * output string with {@code "0"} to make sure the result will always have
     * same length (for a {@link Long}). So it will work when comparing
     * different values as text.
     * 
     * @param n
     *            a long value.
     * @param escapeSql
     *            set to {@code true} if you want to escape SQL for returning
     *            string.
     * @return the formatted string.
     */
    public static String formatNumber(long n, boolean escapeSql) {
        String s = String.format("%020d", n);
        return escapeSql ? DatabaseUtils.sqlEscapeString(s) : s;
    }// formatNumber()

    /**
     * Creates new cursor containing one column
     * {@link android.provider.BaseColumns#_COUNT}.
     * 
     * @return the new cursor.
     */
    public static MatrixCursor newCountCursor() {
        return new MatrixCursor(new String[] { BaseColumns._COUNT });
    }// newCountCursor()

}
